package com.cvte.boe.media.utils;

import java.io.UnsupportedEncodingException;

/**
 * Created by liyang on 2015/3/26.
 *
 * @功能 编码工具类
 */
public class EncodeUtil {

    /**
     * 适配正确的编译字符串
     *
     * @param str
     * @return
     */
    public static String getFitEncodeString(String str) {
        if (StringUtils.isEmpty(str)) return str;
        byte[] bytes = str.getBytes();
        String encodeStr = null;
        try {
            encodeStr = new String(bytes, "unicode");
            LogUtil.d("ENCODE", "unicode");
            if (str.equals(encodeStr))
                return encodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            encodeStr = new String(bytes, "GB2312");
            LogUtil.d("ENCODE", "GB2312");
            if (str.equals(encodeStr))
                return encodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            encodeStr = new String(bytes, "UTF-8");
            LogUtil.d("ENCODE", "UTF-8");
            if (str.equals(encodeStr))
                return encodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            encodeStr = new String(bytes, "ISO-8859-1");
            LogUtil.d("ENCODE", "ISO-8859-1");
            if (str.equals(encodeStr))
                return encodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            encodeStr = new String(bytes, "GBK");
            LogUtil.d("ENCODE", "GBK");
            if (str.equals(encodeStr))
                return encodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        LogUtil.d("ENCODE", "no type");
        return str;
    }

    /**
     * add by zhuhongyue@20151105
     *
     * @功能 识别中文
     */
    public static String getUSBname(String usbLabel) {
        LogUtil.d("EncodeUtils", "the input byte is " + StrToBinstrBlank(usbLabel));
        if (usbLabel.startsWith("M-")) {
            String[] sub = usbLabel.split("M-");
            String ret = new String();
            for (int i = 1; i != sub.length; i++) {
                String temp;
                if (StrToBinstrBlank(sub[i]).split(" ")[0].length() < 7) {
                    temp = "10" + StrToBinstrBlank(sub[i]);
                } else
                    temp = "1" + StrToBinstrBlank(sub[i]);
                ret = new String(ret + temp);
            }
            LogUtil.d("EncodeUtils", "get the out byte is " + ret);
            String strarry = BinstrToStr(ret.toString());
            String test = null;
            try {
                test = new String(strarry.getBytes("ISO-8859-1"), "GBK");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return test;
        } else if (usbLabel.contains("M-")) {
            String[] sub = usbLabel.split("M-");
            String ret = new String();
            for (int i = 1; i != sub.length; i++) {
                String temp;
                if (StrToBinstrBlank(sub[i]).split(" ")[0].length() < 7) {
                    temp = "10" + StrToBinstrBlank(sub[i]);
                } else
                    temp = "1" + StrToBinstrBlank(sub[i]);
                ret = new String(ret + temp);
            }
            LogUtil.d("EncodeUtils", "get the out byte is " + ret);
            String strarry = BinstrToStr(ret.toString());
            String test = null;
            try {
                test = new String(strarry.getBytes("ISO-8859-1"), "GBK");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sub[0] + test;
        } else {
            return usbLabel;
        }
    }

    /**
     * add by zhuhongyue@20151105
     *
     * @功能 编码处理工具
     */
    public static int subCounter(String str1, String str2) {
        int counter = 0;
        for (int i = 0; i <= str1.length() - str2.length(); i++) {
            if (str1.substring(i, i + str2.length()).equalsIgnoreCase(str2)) {
                counter++;
            }
        }
        return counter;
    }

    public static boolean[] StrToBool(String input) {
        boolean[] output = Binstr16ToBool(BinstrToBinstr16(StrToBinstr(input)));
        return output;
    }

    //将bool型数组转换成Unicode字符串
    public static String BoolToStr(boolean[] input) {
        String output = BinstrToStr(Binstr16ToBinstr(BoolToBinstr16(input)));
        return output;
    }

    //将字符串转换成二进制字符串
    public static String StrToBinstr(String str) {
        char[] strChar = str.toCharArray();
        String result = "";
        for (int i = 0; i < strChar.length; i++) {
            result += Integer.toBinaryString(strChar[i]);
        }
        return result;
    }

    public static String StrToBinstrBlank(String str) {
        char[] strChar = str.toCharArray();
        String result = "";
        for (int i = 0; i < strChar.length; i++) {
            result += Integer.toBinaryString(strChar[i]) + " ";
        }
        return result;
    }

    //将二进制字符串转换成Unicode字符串
    public static String BinstrToStr(String binStr) {
        String[] tempStr = StrToStrArray(binStr);
        char[] tempChar = new char[tempStr.length];
        for (int i = 0; i < tempStr.length; i++) {
            tempChar[i] = BinstrToChar(tempStr[i]);
        }
        return String.valueOf(tempChar);
    }

    //将二进制字符串格式化成全16位带空格的Binstr
    public static String BinstrToBinstr16(String input) {
        StringBuffer output = new StringBuffer();
        String[] tempStr = StrToStrArray(input);
        for (int i = 0; i < tempStr.length; i++) {
            for (int j = 16 - tempStr[i].length(); j > 0; j--)
                output.append('0');
            output.append(tempStr[i] + " ");
        }
        return output.toString();
    }

    //将全16位带空格的Binstr转化成去0前缀的带空格Binstr
    public static String Binstr16ToBinstr(String input) {
        StringBuffer output = new StringBuffer();
        String[] tempStr = StrToStrArray(input);
        for (int i = 0; i < tempStr.length; i++) {
            for (int j = 0; j < 16; j++) {
                if (tempStr[i].charAt(j) == '1') {
                    output.append(tempStr[i].substring(j) + " ");
                    break;
                }
                if (j == 15 && tempStr[i].charAt(j) == '0')
                    output.append("0" + " ");
            }
        }
        return output.toString();
    }

    //二进制字串转化为boolean型数组 输入16位有空格的Binstr
    public static boolean[] Binstr16ToBool(String input) {
        String[] tempStr = StrToStrArray(input);
        boolean[] output = new boolean[tempStr.length * 16];
        for (int i = 0, j = 0; i < input.length(); i++, j++)
            if (input.charAt(i) == '1')
                output[j] = true;
            else if (input.charAt(i) == '0')
                output[j] = false;
            else
                j--;
        return output;
    }

    //boolean型数组转化为二进制字串 返回带0前缀16位有空格的Binstr
    public static String BoolToBinstr16(boolean[] input) {
        StringBuffer output = new StringBuffer();
        for (int i = 0; i < input.length; i++) {
            if (input[i])
                output.append('1');
            else
                output.append('0');
            if ((i + 1) % 16 == 0)
                output.append(' ');
        }
        output.append(' ');
        return output.toString();
    }

    //将二进制字符串转换为char
    public static char BinstrToChar(String binStr) {
        int[] temp = BinstrToIntArray(binStr);
        int sum = 0;
        for (int i = 0; i < temp.length; i++) {
            sum += temp[temp.length - 1 - i] << i;
        }
        return (char) sum;
    }

    //将初始二进制字符串转换成字符串数组，以空格相隔
    public static String[] StrToStrArray(String str) {
        return str.split(" ");
    }

    //将二进制字符串转换成int数组
    public static int[] BinstrToIntArray(String binStr) {
        char[] temp = binStr.toCharArray();
        int[] result = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            result[i] = temp[i] - 48;
        }
        return result;
    }
    //add end by zhuhongyue@20151105
}
